﻿using Noke.Common.DatabaseAccess;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Noke.Common.Model
{
    public class BaseModel
    {
        public BaseModel() { }
        public BaseModel(string tableName, string primaryKey, bool isAutoIdentity) { TableName = tableName; PrimaryKeyColumn = primaryKey; IsAutoIdentity = isAutoIdentity; }

        private bool IsAutoIdentity = false;
        private string PrimaryKeyColumn = string.Empty;
        private string TableName = string.Empty;
        private bool EnableUpdateProperty = false;

        private List<string> UpdateProperties = new List<string>();

        public void OpenEnableUpdate()
        {
            UpdateProperties = new List<string>();
            EnableUpdateProperty = true;
        }
        public void CloseEnableUpdate()
        {
            EnableUpdateProperty = false;
        }
        public bool IsNeedUpdate(string UpdateProperty)
        {
            return IsEnableUpdate() && !Has(UpdateProperty);
        }
        internal bool IsEnableUpdate()
        {
            return EnableUpdateProperty;
        }
        internal bool Has(string UpdateProperty)
        {
            return UpdateProperties.Contains(UpdateProperty.ToLower());
        }



        public BaseModel Add(string UpdateProperty)
        {
            UpdateProperties.Add(UpdateProperty.ToLower());
            return this;
        }

        /// <summary>
        /// 获取 insert 语句参数及SQL脚本
        /// </summary>
        /// <returns></returns>
        public DbParameterExtension GetInsertParameters()
        {
            DatabaseConnectionType type = DatabaseAccessConfig.GetInstance().DbConnectionType;
            string prefix = DatabaseAccessConfig.GetInstance().Prefix;

            PropertyInfo[] pInfoList = this.GetType().GetProperties();
            int num = pInfoList.Length;


            if (num <= 0 || type == DatabaseConnectionType.Null)
            {
                return null;
            }

            StringBuilder sqlSb = new StringBuilder();
            DbParameterExtension ape = new DbParameterExtension(num);
            Type attributeType = typeof(PropertyAttribute);
            for (int i = 0; i < num; i++)
            {
                PropertyInfo pInfo = pInfoList[i];
                PropertyAttribute attr = (PropertyAttribute)pInfo.GetCustomAttribute(attributeType, false);
                if (IsAutoIdentity && attr.Equals(PrimaryKeyColumn))
                {
                    continue;
                }
                sqlSb.Append(prefix + attr.ColumnName + (i == (num - 1) ? string.Empty : ","));
                ape.PropertyAttrArr[i] = attr;
                switch (type)
                {
                    case DatabaseConnectionType.SQLSERVER:
                        ape.DbParamArr[i] = new SqlParameter(attr.ColumnName, pInfo.GetValue(this, null));
                        break;
                    //case DatabaseConnectionType.MYSQL:
                    //    ape.DbParamArr[i] = new MySqlParameter(attr.ColumnName, pInfo.GetValue(this, null));
                }
            }
            string sqlColumns = sqlSb.ToString();
            ape.Sql = string.Format(" insert into {0} ({1}) values ({2})", this.TableName, sqlColumns.Replace(prefix, string.Empty), sqlColumns);
            if (IsAutoIdentity)
            {
                ape.Sql += " select @@IDENTITY as ID; ";
            }
            return ape;
        }

    }
}
